home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-02-21 | 18.6 KB | 552 lines | [TEXT/PJMM] |
- unit UGlobals;
-
-
- {-------------------------------------------------------------------------------}
- {#}
- {# Apple Macintosh Developer Technical Support}
- {#}
- {# Interfaces for the application utilities}
- {#}
- {# Program: ProcDoggie}
- {# File: UGlobals.p - Pascal Implementation}
- {#}
- {# by: Forrest Tanaka}
- {#}
- {# Copyright © 1988-1991 Apple Computer, Inc.}
- {# All rights reserved.}
- {#}
- {--------------------------------------------------------------------------------}
- {#}
- {# This unit contains declarations and routines that didn’t seem to fit into any}
- {# other unit in this application.}
- {#}
- {-------------------------------------------------------------------------------}
-
- {-------------------------------------------------------------------------------}
- {#}
- {# 2/21/91 pvh - THINK Pascal conversion.}
- {# Notes:}
- {#}
- {#}
- {-------------------------------------------------------------------------------}
-
- interface
-
-
- (*******************************************************************************}
- {* Used Units}
- {*******************************************************************************)
-
- uses
- (* Group 1 *)
- Types, QuickDraw,
-
- (* Group 2 *)
- Controls, Errors, Events, OSUtils, Files, GestaltEqu, Memory, Resources, TextEdit, ToolUtils,
-
- (* Group 3 *)
- Notification, OSEvents, Processes, Windows,
-
- (* Group 4 *)
- Dialogs,
-
- (* Application *)
- UEmergMem;
-
-
- (*******************************************************************************}
- {* Constants}
- {*******************************************************************************)
-
- const
- rMemErrMessages = 1000; {Resource ID of memory error message STR#}
- kMemErrAppOpenMsg = 1; {Not enough memory to open application}
- kMemErrProcListOpenMsg = 2; {Not enough mem to open process list wind}
- kMemErrLowMemWarnMsg = 3; {Free memory is low; proceed with caution}
- kMemErrProcInfoOpenMsg = 4; {Not enough mem to open process info wind}
-
- rResErrMessages = 1001; {Resource ID of resource error message STR#}
- kResErrAppDamageMsg = 1; {Application is damaged}
-
- rMiscErrMessages = 1002; {Resource ID of misc. error message STR#}
- kMiscErrUnknownMsg = 1; {Unknown error}
-
- rMiscWrnMessages = 2000; {Resource ID of misc. warning message STR#}
- kMiscWrnUncleanMsg = 1; {Application not 32-bit clean warning}
- kMiscWrnLaunchMemMsg = 2; {Not enough memory to launch}
-
- kMaxSleepTime = 60; {Max WNE sleep time in ticks, 1 second in this case}
-
-
- (*******************************************************************************}
- {* Global Variables}
- {*******************************************************************************)
-
- var
- gError: Integer; {Generic application error code}
- gWereInFront: Boolean; {TRUE if this appliation is front most}
- gQuitting: Boolean; {TRUE if user chose Quit command}
-
-
- (*******************************************************************************}
- {* DoQuit - Handle Quit command}
- {*}
- {* This routine is called when this application should quit. It’s called}
- {* when the user chooses the Quit command from the file menu or when a 'quit'}
- {* AppleEvent is received.}
- {*******************************************************************************)
-
- procedure DoQuit;
-
-
- (*******************************************************************************}
- {* ShowStopAlert - Show a stop alert}
- {*}
- {* This routine puts up a standard stop alert with just an OK button and a}
- {* specified message. messageClass specifies the STR# resource ID which contains}
- {* the message to display and messageIndex specifies the index (the first message}
- {* is index 1) into that STR# of the message to display.}
- {*******************************************************************************)
-
- function ShowStopAlert (messageClass: Integer; messageIndex: Integer): Integer;
-
-
- (*******************************************************************************}
- {* ShowCautionOKCancelAlert - Show a caution alert with an OK and Cancel button}
- {*}
- {* This routine puts up a standard caution alert with just an OK button, a Cancel}
- {* button, and a specified message. messageClass specifies the STR# resource ID}
- {* which contains the message to display and messageIndex specifies the index}
- {* (the first message is index 1) into that STR# of the message to display.}
- {*******************************************************************************)
-
- function ShowCautionOKCancelAlert (messageClass: Integer; messageIndex: Integer): Integer;
-
-
- (*******************************************************************************}
- {* ShowCautionOKAlert - Show a caution alert with just an OK button}
- {*}
- {* This routine puts up a standard caution alert with just an OK button and a}
- {* specified message. messageClass specifies the STR# resource ID which contains}
- {* the message to display and messageIndex specifies the index (the first message}
- {* is index 1) into that STR# of the message to display.}
- {*******************************************************************************)
-
- function ShowCautionOKAlert (messageClass: Integer; messageIndex: Integer): Integer;
-
-
- (*******************************************************************************}
- {* ShowAboutBox - Show the About box}
- {*}
- {* The About box for this application is displayed. This is just a simple alert}
- {* box with an OK button. The name of this application is displayed as is its}
- {* version number.}
- {*******************************************************************************)
-
- procedure ShowAboutBox;
-
-
- (*******************************************************************************}
- {* CreateWindow - Create a window}
- {*}
- {* This routine creates a window using the WIND resource template with a resource}
- {* ID of windowTmplID in front of all existing windows. A pointer to this window}
- {* is returned. If we’re running on a Color QuickDraw machine, the window is}
- {* created as a color window. This window must be disposed of by calling}
- {* CloseWindow followed by DisposPtr on the window’s WindowRecord. The window}
- {* shouldn’t be closed using DisposeWindow. When this routine returns, the new}
- {* window is the current GrafPort.}
- {*}
- {* The window isn’t explicitly repositioned from its position as defined by the}
- {* resource definition. Instead, I use the window-positioning utilities. This}
- {* isn’t documented at this time and it isn’t even certain the these utilities}
- {* will be implemented in the final release of system software 7.0. Use ResEdit}
- {* 2.1’s WIND editor to control the window-positioning utilities.}
- {*}
- {* If there isn’t enough memory for the window, then gError global is set to}
- {* memFullErr. If the WIND resource couldn’t be loaded, gError is set to}
- {* resNotFound. If anything else went wrong while creating the window, then}
- {* gError is set to dsSysErr.}
- {*******************************************************************************)
-
- function CreateWindow (windowTmplID: Integer): WindowPtr;
-
-
- (*******************************************************************************}
- {* CreateDialog - Create a dialog window}
- {*}
- {* This routine creates a dialog window using the WIND resource template with a}
- {* resource ID of windowTmplID in front of all existing windows. A pointer to}
- {* this window is returned. If we’re running on a Color QuickDraw machine, the}
- {* window is created as a color window. This window must be disposed of by}
- {* calling CloseWindow followed by DisposPtr on the window’s DialogRecord. The}
- {* window shouldn’t be closed using DisposeWindow. When this routine returns,}
- {* the new dialog is the current GrafPort.}
- {*}
- {* The window isn’t explicitly repositioned from its position as defined by the}
- {* resource definition. Instead, I use the window-positioning utilities. This}
- {* isn’t documented at this time and it isn’t even certain the these utilities}
- {* will be implemented in the final release of system software 7.0. Use ResEdit}
- {* 2.1’s WIND editor to control the window-positioning utilities.}
- {*}
- {* If there isn’t enough memory for the window, then gError global is set to}
- {* memFullErr. If the WIND resource couldn’t be loaded, gError is set to}
- {* resNotFound. If anything else went wrong while creating the window, then}
- {* gError is set to dsSysErr.}
- {*******************************************************************************)
-
- function CreateDialog (dialogTmplID: Integer): DialogPtr;
-
-
- implementation
-
- (*******************************************************************************}
- {* Constants}
- {*******************************************************************************)
-
- const
- rOKAlertID = 6010; {Resource ID of alert with OK button}
- rOKCancelAlertID = 6011; {Resource ID of alert with OK and Cancel buttons}
-
- rIconSuiteID = 128; {Resource ID of application icon suite}
- rAppNameString = 0; {Resource ID of application name}
- rAboutAlert = 258; {Resource ID of About alert}
-
-
- (*******************************************************************************}
- {* Variables}
- {*******************************************************************************)
-
- var
- gNotification: NMRec; {Notification record}
-
-
- {$S Main}
- (*******************************************************************************}
- {* Public: DoQuit}
- {*}
- {* The gQuitting global variable is set to TRUE. This causes the main event loop}
- {* to terminate on the next iteration.}
- {*******************************************************************************)
-
- procedure DoQuit;
-
- begin
- gQuitting := TRUE
- end;
-
-
- {$S Main}
- (*******************************************************************************}
- {* Private: NotifyAlert - Present a notification for an alert}
- {*}
- {* This routine is called to present a notification to the user in the form of a}
- {* flashing icon in the application menu whenever an alert from this program is}
- {* displayed. It’s only necessary to present a notification if this application}
- {* is in the background, so the first check that’s done is to compare our own}
- {* process serial number against the process serial number of the foreground}
- {* process. If they’re the same, then this application is in the foreground and}
- {* no notification is needed. If they’re not the same, then the notification is}
- {* presented.}
- {*}
- {* After this application is brought to the front and the alert is dismissed this}
- {* notification is removed by the routine that calls NotifyAlert.}
- {*******************************************************************************)
-
- procedure NotifyAlert;
-
- var
- iconSuite: Handle; {Handle to the icon suite}
- osEvent: EventRecord; {OS event for resume}
- error: OSErr;
-
- procedure RecoverError (errorCode: OSErr);
-
- begin
- EXIT(NotifyAlert)
- end;
-
- begin
- (* If this application isn’t in front, post a notification *)
- if not gWereInFront then
- begin
- (* Get the small icon for this application *)
- iconSuite := Get1Resource('SICN', rIconSuiteID);
- with gNotification do
- begin
- (*WITH*)
- qType := ORD(nmtype);
- (*WITH*)
- nmMark := 1;
- (*WITH*)
- nmIcon := iconSuite;
- (*WITH*)
- nmSound := Handle(-1);
- (*WITH*)
- nmStr := nil;
- (*WITH*)
- nmResp := nil;
- (*WITH*)
- nmRefCon := 0
- end;
-
- (* Post the notification *)
- error := NMINstall(@gNotification);
-
- (* Wait for a resume event *)
- while not WaitNextEvent(app4Mask, osEvent, -1, nil) do (*<*)
- ;
- gWereInFront := TRUE;
- error := NMRemove(@gNotification);
-
- (* Notification blows away pending update events, so force *)
- InvalRect(FrontWindow^.portRect);
- end
- end;
-
-
- {$S Main}
- (*******************************************************************************}
- {* Public: ShowStopAlert}
- {*}
- {* NotifyAlert is called to present a notification to the user in case this}
- {* application is in the background at the time of the alert. This routine then}
- {* removes the notification after StopAlert is called.}
- {*******************************************************************************)
-
- function ShowStopAlert (messageClass: Integer; messageIndex: Integer): Integer;
-
- var
- aMessage: Str255; {Contents of message to place in alert}
-
- begin
- (* Put the specified message into the dialog parameter text *)
- GetIndString(aMessage, messageClass, messageIndex);(*<*)
- ParamText(aMessage, '', '', '');
-
- (* Show the stop alert *)
- InitCursor;
- NotifyAlert;
- ShowStopAlert := StopAlert(rOKAlertID, nil);
- end;
-
-
- {$S Main}
- (*******************************************************************************}
- {* Public: ShowCautionOKCancelAlert}
- {*}
- {* NotifyAlert is called to present a notification to the user in case this}
- {* application is in the background at the time of the alert. This routine then}
- {* removes the notification after ShowCautionOKCancelAlert is called.}
- {*******************************************************************************)
-
- function ShowCautionOKCancelAlert (messageClass: Integer; messageIndex: Integer): Integer;
-
- var
- aMessage: Str255; {Contents of message to place in alert}
-
- begin
- (* Put the specified message into the dialog parameter text *)
- GetIndString(aMessage, messageClass, messageIndex);(*<*)
- ParamText(aMessage, '', '', '');
-
- (* Show the stop alert *)
- InitCursor;
- NotifyAlert;
- ShowCautionOKCancelAlert := CautionAlert(rOKCancelAlertID, nil)
- end;
-
-
- {$S Main}
- (*******************************************************************************}
- {* Public: ShowCautionOKAlert}
- {*}
- {* NotifyAlert is called to present a notification to the user in case this}
- {* application is in the background at the time of the alert. This routine then}
- {* removes the notification after ShowCautionOKAlert is called.}
- {*******************************************************************************)
-
- function ShowCautionOKAlert (messageClass: Integer; messageIndex: Integer): Integer;
-
- var
- aMessage: Str255; {Contents of message to place in alert}
- error: OSErr;
-
- begin
- (* Put the specified message into the dialog parameter text *)
- GetIndString(aMessage, messageClass, messageIndex);(*<*)
- ParamText(aMessage, '', '', '');
-
- (* Show the stop alert *)
- InitCursor;
- NotifyAlert;
- ShowCautionOKAlert := CautionAlert(rOKAlertID, nil);
- error := NMRemove(@gNotification)
- end;
-
-
- {$S Main}
- (*******************************************************************************}
- {* Public: ShowAboutBox}
- {*}
- {* The name and the version number of this application are retrieved from}
- {* resources. They’re then displayed in the About box using the ParamText}
- {* mechanism.}
- {*******************************************************************************)
-
- procedure ShowAboutBox;
-
- var
- appNameRes: StringHandle; {Handle to name of application}
- curVersion: VersRecHndl; {Handle to version record of this app}
- appName: Str255; {Name of this application}
- verNum: Str255; {Long version number of this application}
- itemHit: Integer; {Item number of clicked alert item; ignored}
-
- begin
- (* Get the name of this application *)
- appNameRes := GetString(rAppNameString);
- if appNameRes <> nil then
- appName := appNameRes^^
- else
- appName := '?';
-
- (* Get the version information of this application *)
- curVersion := VersRecHndl(Get1Resource('vers', 1));
- if curVersion <> nil then
- (* Get the long version number *)
- verNum := StringPtr(ORD(@curVersion^^.shortVersion) + ORD(curVersion^^.shortVersion[0]) + 1)^
- else
- verNum := '?';
-
- (* Show the About alert *)
- ParamText(appName, verNum, '', '');
- itemHit := Alert(rAboutAlert, nil);
- end;
-
-
- {$S Main}
- (*******************************************************************************}
- {* Public: CreateWindow}
- {*}
- {* A WindowRecord is allocated on the heap before GetNewWindow is called. This}
- {* is done to reduce heap fragmentation.}
- {*******************************************************************************)
-
- function CreateWindow (windowTmplID: Integer): WindowPtr;
-
- var
- windStore: Ptr; {Pointer to window record}
- aWindow: WindowPtr; {Pointer to the new window}
- qdVersion: LongInt; {Version of QuickDraw on this machine}
- result: OSErr;
-
- procedure RecoverError (error: Integer);
-
- begin
- if aWindow <> nil then
- CloseWindow(aWindow);
- if windStore <> nil then
- DisposPtr(windStore);
- gError := error;
- CreateWindow := nil;
- EXIT(CreateWindow)
- end;
-
- begin
- windStore := nil;
- aWindow := nil;
-
- (* Allocate window record *)
- windStore := NewPtrMargin(SIZEOF(WindowRecord), kAllocApp, not kAllocClr);
-
- (* Create the new window *)
- if windStore = nil then
- RecoverError(memFullErr)
- else
- begin
- (* Create the new window *)
- result := Gestalt(gestaltQuickdrawVersion, qdVersion); (*<*)
- if qdVersion = gestaltOriginalQD then
- aWindow := GetNewWindow(windowTmplID, windStore, WindowPtr(-1))
- else
- aWindow := GetNewCWindow(windowTmplID, windStore, WindowPtr(-1));
- if FailLowMemory(0) then
- RecoverError(memFullErr)
- else if ResError <> noErr then
- if ResError = memFullErr then
- RecoverError(memFullErr)
- else if (ResError = noErr) | (ResError = resNotFound) then
- RecoverError(resNotFound)
- else
- RecoverError(dsSysErr);
-
- SetPort(aWindow);
- SetWRefCon(aWindow, 0)
- end;
- CreateWindow := aWindow
- end;
-
-
- {$S Main}
- (*******************************************************************************}
- {* Public: CreateDialog}
- {*}
- {* A DialogRecord is allocated on the heap before GetNewWindow is called. This}
- {* is done to reduce heap fragmentation.}
- {*******************************************************************************)
-
- function CreateDialog (dialogTmplID: Integer): DialogPtr;
-
- var
- dlogStore: Ptr; {Pointer to dialog record}
- aDialog: DialogPtr; {Pointer to the new dialog window}
- qdVersion: LongInt; {Version of QuickDraw on this machine}
- result: OSErr;
-
- procedure RecoverError (error: Integer);
-
- begin
- if aDialog <> nil then
- CloseWindow(aDialog);
- if dlogStore <> nil then
- DisposPtr(dlogStore);
- gError := error;
- CreateDialog := nil;
- EXIT(CreateDialog)
- end;
-
- begin
- dlogStore := nil;
- aDialog := nil;
-
- (* Allocate window record *)
- dlogStore := NewPtrMargin(SizeOf(DialogRecord), kAllocApp, not kAllocClr);
-
- (* Create the new window *)
- if dlogStore = nil then
- RecoverError(memFullErr)
- else
- begin
- result := Gestalt(gestaltQuickdrawVersion, qdVersion); (*<*)
- if qdVersion = gestaltOriginalQD then
- aDialog := GetNewWindow(dialogTmplID, dlogStore, WindowPtr(-1))
- else
- aDialog := GetNewCWindow(dialogTmplID, dlogStore, WindowPtr(-1));
- if FailLowMemory(0) then
- RecoverError(memFullErr)
- else if ResError <> noErr then
- if ResError = memFullErr then
- RecoverError(memFullErr)
- else if (ResError = noErr) | (ResError = resNotFound) then
- RecoverError(resNotFound)
- else
- RecoverError(dsSysErr);
-
- SetPort(aDialog);
- SetWRefCon(aDialog, 0)
- end;
- CreateDialog := aDialog
- end;
-
- end.